package io.gitee.zkool.barcodegun.pc.swing;

import cn.hutool.core.net.NetUtil;
import io.gitee.zkool.barcodegun.pc.constant.CommonConstant;
import io.gitee.zkool.barcodegun.pc.ws.WsServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.*;

/**
 * @author zhaokk
 * @since 2024/2/20
 */
public class SwingClient {

    private static Logger logger = LoggerFactory.getLogger(SwingClient.class);
    public static final SwingClient SWINGCLIENT;

    static {
        // Look and Feel
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            logger.error("Erro ao configurar NimbusLookAndFeel");
        }

        // Esse painel do form principal está sendo usando em outros lugares da aplicação
        SWINGCLIENT = new SwingClient();
    }

    public JFrame frame;
    public JPanel contentPane;
    public JTextPane jTextPane;
    public JScrollPane logScrollPane;
    public JLabel lableApplicationStatus;


    //实例化一个监听服务器
    private WsServer ws = new WsServer();

    /**
     * Create the application.
     */
    public SwingClient() {
        initialize();
        ws.start();
    }

    public void initialize() {
        frame = new JFrame(CommonConstant.APP_NAME + " " + CommonConstant.APP_VERSION);
        // set window size
        frame.setBounds(0, 0, 1000, 800);
        // Set the default window closing method
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        // Set the window to the center of the screen
        frame.setLocationRelativeTo(null);
        contentPane = new JPanel(new BorderLayout());
        contentPane.setBackground(Color.WHITE);
        jTextPane = new JTextPane();
        // Settings are not editable
        jTextPane.setEditable(false);

        // show with ScrollPane
        logScrollPane = new JScrollPane();
        logScrollPane.setBounds(30, 50, 900, 500);
        logScrollPane.setViewportView(jTextPane);

        frame.setContentPane(contentPane);


        JLabel lableHead = new JLabel("WebSocket Server  ws://" + NetUtil.getLocalhostStr() + ":" + ws.getPort(), JLabel.CENTER);
        lableHead.setFont(new Font("Calibri", Font.PLAIN, 20));
        lableHead.setBounds(0, 100, 20, 30);
        contentPane.add("North", lableHead);

        contentPane.add("Center", logScrollPane);
        lableApplicationStatus = new JLabel("QQ : 2459501893", JLabel.CENTER);
        lableApplicationStatus.setFont(new Font("Calibri", Font.PLAIN, 18));
        lableApplicationStatus.setBounds(0, 100, 20, 15);
        contentPane.add("South", lableApplicationStatus);

    }

    public JTextPane getTextPane() {
        return jTextPane;
    }

    public JFrame getFrame() {
        return frame;
    }

    public void setFrame(JFrame frame) {
        this.frame = frame;
    }

}
